package com.happy.study.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.happy.study.entity.PaperEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 20:32 2021/1/26
 * @return
 */
@Mapper
public interface PaperMapper extends BaseMapper<PaperEntity> {


    @Select ( value = "<script>SELECT pi.paperId,\n" +
            "       pi.paperName,\n" +
            "       pi.level,\n" +
            "       pi.number,\n" +
            "       pi.categoryId,\n" +
            "       ci.categoryName AS categoryName,\n" +
            "       pi.createTime,\n" +
            "       pi.changeTime,\n" +
            "       pi.isUsed\n" +
            "FROM paper_info pi\n" +
            "         left join category_info ci on ci.id = pi.categoryId\n" +
            "<where><if test='paperEntity.level != null and paperEntity.level != 0'>and pi.level=#{paperEntity.level}</if>\n" +
            "  <if test='paperEntity.categoryId != null and paperEntity.categoryId != 0'>and pi.categoryId=#{paperEntity.categoryId}</if></where> order by pi.categoryId ASC limit #{start}, #{end}</script>" )
    List<PaperEntity> queryList ( @Param ( "paperEntity" ) PaperEntity paperEntity, @Param ( "start" ) int start, @Param ( "end" ) int end );


    @Select ( value = "<script>SELECT count(*) FROM paper_info pi\n" +
            "         left join category_info ci on ci.id = pi.categoryId\n" +
            "<where><if test='paperEntity.level != null and paperEntity.level != 0'>and pi.level=#{paperEntity.level}</if>\n" +
            "  <if test='paperEntity.categoryId != null and paperEntity.categoryId != 0'>and pi.categoryId=#{paperEntity.categoryId}</if></where></script>" )
    int queryCount ( @Param ( "paperEntity" ) PaperEntity paperEntity );

    /**
     * 查询单条记录
     *
     * @param paperId
     * @return
     */
    @Select ( value = "SELECT pi.paperId,\n" +
            "       pi.paperName,\n" +
            "       pi.level,\n" +
            "       pi.number,\n" +
            "       pi.categoryId,\n" +
            "       ci.categoryName AS categoryName,\n" +
            "       pi.createTime,\n" +
            "       pi.changeTime,\n" +
            "       pi.isUsed\n" +
            "FROM paper_info pi\n" +
            "         left join category_info ci on ci.id = pi.categoryId\n" +
            "where pi.paperId=#{paperId}" )
    PaperEntity querySingle ( int paperId );


}
